package utils;

import java.util.List;

import org.apache.log4j.Logger;

import weibo4j.Paging;
import weibo4j.Status;
import weibo4j.Weibo;
import weibo4j.WeiboException;

public class GetUserTimeline {
	
	private static final Logger log = Logger.getLogger(GetUserTimeline.class);

	private Weibo weibo = null;
	private StartPoint sp = new StartPoint();
	
	public GetUserTimeline(Weibo weibo){
		this.weibo = weibo;
	}
	
	/**
	 * 获取最新更新的公共微博消息
	 * @param args
	 */
	public List<Status> getUserTimeline (String userId) {

		try {
			Paging pag = new Paging();
			String userBlogStart = sp.get("userBlogStart");
			if (userBlogStart == null || "".equals(userBlogStart)){
				userBlogStart = "1";
			}
			long sinceId = Long.parseLong(userBlogStart);
			pag.setSinceId(sinceId);
			pag.setCount(200);
			
			//获取24小时内前20条用户的微博信息;
			List<Status> statuses = weibo.getUserTimeline(userId,pag);
			for(Status status: statuses){
				if (status.getId() > sinceId)
					sinceId = status.getId();
			}
			
			sp.save("userBlogStart", String.valueOf(sinceId));
			Thread.currentThread().sleep(1000 * 60 * 2);
			
			log.info(statuses);
			
			return statuses;
		} catch (Exception e) {
			log.debug(e);
		}
		
		return null;
	}
}
